﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace BattleNet
{
    class BattleNetScraper
    {
        public string GetClass(string character, string server)
        {
            string path = @"http://eu.battle.net/wow/en/character/quelthalas/enion/simple";
            path = path = @"http://eu.battle.net/wow/en/character/" + server + @"/" + character + @"/simple";

            string result = string.Empty;
            List<string> specs = new List<string>();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
                request.Method = "GET";
                using (var stream = request.GetResponse().GetResponseStream())
                {
                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                }

                int start = result.IndexOf("class=\"class\">")+14;
                int end = result.IndexOf("</a>", start);
                StringBuilder sb = new StringBuilder();
                for (int i = start; i < end; i++)
                    sb.Append(result[i]);

                string classs = sb.ToString();


                return classs;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        public List<FactionDetail> GetReputation(string character, string server)
        {
            string path = @"http://eu.battle.net/wow/en/character/quelthalas/enion/reputation/";
            path = @"http://eu.battle.net/wow/en/character/" + server + @"/" + character + @"/reputation/";
            

            string result = string.Empty;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
                request.Method = "GET";
                using (var stream = request.GetResponse().GetResponseStream())
                {
                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                }

                //HTML parsing:            
                HtmlDocument doc = new HtmlDocument();
                HtmlDocument factionDetails = new HtmlDocument();
                doc.Load(new StringReader(result));
                HtmlNode root = doc.DocumentNode;

                List<string> rep = new List<string>();
                List<string> faction = new List<string>();
                List<FactionDetail> factionDetailList = new List<FactionDetail>();

                foreach (HtmlNode link in root.SelectNodes("//li"))
                {

                    string att = link.OuterHtml;
                    string ih = link.InnerHtml;

                    string xp = link.XPath;
                    factionDetails.Load(new StringReader(link.InnerHtml));
                    if (link.HasAttributes == true)
                    {
                        if (link.Attributes[0].Value == "faction-details")
                        {
                            string nameStr = null, repStr = null, statusStr = null;
                            string[] repStrArr = new string[2];
                            FactionDetail factionDetail = new FactionDetail();
                            foreach (HtmlNode detail in factionDetails.DocumentNode.SelectNodes("//div"))
                            {
                                if (detail.HasAttributes == true)
                                {
                                    if (detail.Attributes[0].Value == "faction-score")
                                    {
                                        repStr = Regex.Replace(detail.InnerText, @"\s", "");
                                        repStrArr = repStr.Split('/');
                                    }
                                    if (detail.Attributes[0].Value == "faction-level")
                                    {
                                        statusStr = Regex.Replace(detail.InnerText, @"\s", "");
                                    }
                                }
                            }
                            foreach (HtmlNode detail in factionDetails.DocumentNode.SelectNodes("//span"))
                            {
                                if (detail.HasAttributes == true)
                                {
                                    if (detail.Attributes[0].Value == "faction-name")
                                    {
                                        nameStr = Regex.Replace(detail.InnerText, @"\s", "");
                                        faction.Add(nameStr);
                                    }
                                }
                            }
                            if (repStrArr[0] != null)
                            {
                                factionDetail.Name = nameStr;
                                factionDetail.Rep = int.Parse(repStrArr[0]);
                                factionDetail.RepOf = int.Parse(repStrArr[0]);
                                factionDetail.Status = statusStr;
                                factionDetailList.Add(factionDetail);
                            }
                        }

                    }

                }
                return factionDetailList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }



        public class FactionDetail
        {
            private string _name = null;
            private string _status = null;
            private int _rep = 0;
            private int _repOf = 0;
            private int _repLevel = 0;
            private string _expansion;

            public string Name
            {
                get { return _name; }
                set 
                {
                    _name = value;
                    // The names are not 'clean' from the battle.net site, so clean them before saving
                    switch (_name)
                    {
                        case ("Baradin&#39;sWardens"):
                            _name="Baradin's Wardens";
                            _expansion = "Cataclysm";
                            break;
                        case ("GuardiansofHyjal"):
                            _name = "Guardians of Hyjal";
                            _expansion = "Cataclysm";
                            break;
                        case ("Hellscream&#39;sReach"):
                            _name = "Hellscream's Reach";
                            _expansion = "Cataclysm";
                            break;
                        case ("Ramkahen"):
                            _expansion = "Cataclysm";
                            break;
                        case ("TheEarthenRing"):
                            _name = "The Earthen Ring";
                            _expansion = "Cataclysm";
                            break;
                        case ("Therazane"):
                            _expansion = "Cataclysm";
                            break;
                        case ("WildhammerClan"):
                            _name = "Wildhammer Clan";
                            _expansion = "Cataclysm";
                            break;
                        case ("ArgentCrusade"):
                            _name = "Argent Crusade";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("KirinTor"):
                            _name = "Kirin Tor";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("KnightsoftheEbonBlade"):
                            _name = "Knights of the Ebon Blade";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheAshenVerdict"):
                            _name = "The Ashen Verdict";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheKalu&#39;ak"):
                            _name = "The Kalu'ak";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheSonsofHodir"):
                            _name = "The Sons of Hodir";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheWyrmrestAccord"):
                            _name = "The Wyrmrest Accord";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("Explorers&#39;League"):
                            _name = "Explorers' League";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheFrostborn"):
                            _name = "The Frostborn";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheSilverCovenant"):
                            _name = "The Silver Covenant";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("ValianceExpedition"):
                            _name = "Valiance Expedition";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("FrenzyheartTribe"):
                            _name = "Frenzyheart Tribe";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("TheOracles"):
                            _name = "The Oracles";
                            _expansion = "Wrath of the Lich King";
                            break;
                        case ("AshtongueDeathsworn"):
                            _name = "Ashtongue Deathsworn";
                            _expansion = "Burning Crusade";
                            break;
                        case ("CenarionExpedition"):
                            _name = "Cenarion Expedition";
                            _expansion = "Burning Crusade";
                            break;
                        case ("HonorHold"):
                            _name = "Honor Hold";
                            _expansion = "Burning Crusade";
                            break;
                        case ("KeepersofTime"):
                            _name = "Keepers of Time";
                            _expansion = "Burning Crusade";
                            break;
                        case ("Kurenai"):
                            _expansion = "Burning Crusade";
                            break;
                        case ("Netherwing"):
                            _expansion = "Burning Crusade";
                            break;
                        case ("Ogri&#39;la"):
                            _name = "Ogri'la";
                            _expansion = "Burning Crusade";
                            break;
                        case ("Sporeggar"):
                            _expansion = "Burning Crusade";
                            break;
                        case ("TheConsortium"):
                            _name = "The Consortium";
                            _expansion = "Burning Crusade";
                            break;
                        case ("TheScaleoftheSands"):
                            _name = "The Scale of the Sands";
                            _expansion = "Burning Crusade";
                            break;
                        case ("TheVioletEye"):
                            _name = "The Violet Eye";
                            _expansion = "Burning Crusade";
                            break;
                        case ("LowerCity"):
                            _name = "Lower City";
                            _expansion = "Burning Crusade";
                            break;
                        case ("Sha&#39;tariSkyguard"):
                            _name = "Sha'tari Skyguard";
                            _expansion = "Burning Crusade";
                            break;
                        case ("ShatteredSunOffensive"):
                            _name = "Shattered Sun Offensive";
                            _expansion = "Burning Crusade";
                            break;
                        case ("TheAldor"):
                            _name = "The Aldor";
                            _expansion = "Burning Crusade";
                            break;
                        case ("TheScryers"):
                            _name = "The Scryers";
                            _expansion = "Burning Crusade";
                            break;
                        case ("TheSha&#39;tar"):
                            _name = "The Sha'tar";
                            _expansion = "Burning Crusade";
                            break;
                        case ("ArgentDawn"):
                            _name = "Argent Dawn";
                            _expansion = "Classic";
                            break;
                        case ("BloodsailBuccaneers"):
                            _name = "Bloodsail Buccaneers";
                            _expansion = "Classic";
                            break;
                        case ("BroodofNozdormu"):
                            _name = "Brood of Nozdormu";
                            _expansion = "Classic";
                            break;
                        case ("CenarionCircle"):
                            _name = "Cenarion Circle";
                            _expansion = "Classic";
                            break;
                        case ("DarkmoonFaire"):
                            _name = "Darkmoon Faire";
                            _expansion = "Classic";
                            break;
                        case ("GelkisClanCentaur"):
                            _name = "Gelkis Clan Centaur";
                            _expansion = "Classic";
                            break;
                        case ("Hydraxian Waterlords"):
                            _name = "Hydraxian Waterlords";
                            _expansion = "Classic";
                            break;
                        case ("MagramClanCentaur"):
                            _name = "Magram Clan Centaur";
                            _expansion = "Classic";
                            break;
                        case ("Ravenholdt"):
                            _expansion = "Classic";
                            break;
                        case ("Shen&#39;dralar"):
                            _name = "Shen'dralar";
                            _expansion = "Classic";
                            break;
                        case ("ThoriumBrotherhood"):
                            _name = "ThoriumBrotherhood";
                            _expansion = "Classic";
                            break;
                        case ("TimbermawHold"):
                            _name = "Timbermaw Hold";
                            _expansion = "Classic";
                            break;
                        case ("ZandalarTribe"):
                            _name = "Zandalar Tribe";
                            _expansion = "Classic";
                            break;
                        case ("Exodar"):
                            _expansion = "Classic";
                            break;
                        case ("Gilneas"):
                            _expansion = "Classic";
                            break;
                        case ("Gnomeregan"):
                            _expansion = "Classic";
                            break;
                        case ("Ironforge"):
                            _expansion = "Classic";
                            break;
                        case ("Stormwind"):
                            _expansion = "Classic";
                            break;
                        case ("SilverwingSentinels"):
                            _name = "Silverwing Sentinels";
                            _expansion = "Classic";
                            break;
                        case ("StormpikeGuard"):
                            _name = "Stormpike Guard";
                            _expansion = "Classic";
                            break;
                        case ("TheLeagueofArathor"):
                            _name = "The League of Arathor";
                            _expansion = "Classic";
                            break;
                        case ("BootyBay"):
                            _name = "Booty Bay";
                            _expansion = "Classic";
                            break;
                        case ("Everlook"):
                            _expansion = "Classic";
                            break;
                        case ("Gadgetzan"):
                            _expansion = "Classic";
                            break;
                        case ("Ratchet"):
                            _expansion = "Classic";
                            break;
                        case ("KnightsOfNi"):
                            _name = "Knights Of Ni";
                            _expansion = "Cataclysm";
                            break;
                        case ("Syndicate"):
                            _name = "Syndicate";
                            _expansion = "Classic";
                            break;
                    }
                }
            }
            public string Status
            {
                get { return _status; }
                set { _status = value; }
            }
            public int Rep
            {
                get { return _rep; }
                set { _rep = value; }
            }
            public int RepOf
            {
                get { return _repOf; }
                set
                { _repOf = value; }
            }
            public int RepLevel
            {
                get
                {
                    if (_status!=null)
                    {
                        switch (_status)
                        {
                            case "Exalted":
                                _repLevel=7;
                                break;
                            case "Revered":
                                _repLevel=6;
                                break;
                            case "Honored":
                                _repLevel=5;
                                break;
                            case "Friendly":
                                _repLevel=4;
                                break;
                            case "Neutral":
                                _repLevel=3;
                                break;
                            case "Unfriendly":
                                _repLevel=2;
                                break;
                            case "Hostile":
                                _repLevel=1;
                                break;
                            case "Hated":
                                _repLevel=0;
                                break;
                            default:
                                throw new Exception ("Unknown reputation");
                        }
                    }
                    else 
                        throw new Exception("Set reputation status prior to getting RepLevel");
                    return _repLevel;
                }
            }


        }

        

    }
}
